/**
 * 
 */
package com.teamall.web.family.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.teamall.core.common.Page;
import com.teamall.core.convert.IArgConversionService;
import com.teamall.core.idproduce.ISequenceGenerator;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.log.LogManager;
import com.teamall.framework.util.DateUtils;
import com.teamall.framework.util.Utils;
import com.teamall.web.family.db.arg.FamilyIncomeArg;
import com.teamall.web.family.db.arg.FamilyIncomeArg.FamilyIncomeCriteria;
import com.teamall.web.family.db.dao.FamilyIncomeDao;
import com.teamall.web.family.db.po.FamilyIncomePO;
import com.teamall.web.family.service.IFamilyIncomeService;

/**
 * <Description> <br>
 * 
 * @author codeCreater<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014年11月11日 <br>
 * @since V1.0<br>
 * @see com.teamall.web.family.service.impl <br>
 */

@Service("familyIncomeService")
public class FamilyIncomeServiceImpl implements IFamilyIncomeService {

    private static final LogManager logger = LogManager
            .getLogger(FamilyIncomeServiceImpl.class);

    @Autowired
    private FamilyIncomeDao familyIncomeDao;

    /**
     * 查询条件转换成Arg类的服务接口
     */
    @Resource(name = "defaultArgConversionService")
    private IArgConversionService argConversionService;

    /**
     * 主键生成器
     */
    @Resource(name = "sequenceProcGenerator")
    private ISequenceGenerator sequenceGenerator;

    @Override
    public FamilyIncomePO selectByPrimaryKey(Integer key)
            throws BaseAppException {
        // ///////
        // TODO 根据业务场景，设置查询条件、数据校验等

        // ///////
        return familyIncomeDao.selectByPrimaryKey(key);
    }

    @Override
    public List<FamilyIncomePO> selectByArg(FamilyIncomePO record)
            throws BaseAppException {
        logger.debug("selectByArg begin...record={0}", record);

        // 第一种方式：自己创建arg，自行设置查询条件及操作符
        FamilyIncomeArg arg = new FamilyIncomeArg();
        FamilyIncomeCriteria criteria = arg.createCriteria();

        // ///////
        // TODO 根据业务场景，设置查询条件，示例
        if (Utils.notEmpty(record.getFamilyId())) {
            criteria.andFamilyIdEqualTo(record.getFamilyId());
        }
        if (Utils.notEmpty(record.getStatDate())) {
            String baseDate = DateUtils.date2String(record.getStatDate(),
                    DateUtils.STR_DATE_FORMAT_YEAR);
            criteria.andStatDateBetween(DateUtils.string2Date(
                    baseDate + "0101",
                    DateUtils.STR_DATE_FORMAT_DAY_WITHOUT_SPLIT), DateUtils
                    .string2Date(baseDate + "1231235959",
                            DateUtils.STR_DEFAULT_DATE_FORMAT_WITHOUT_SPLIT));
        }
        if (Utils.notEmpty(record.getStatQuarter())) {
            criteria.andStatQuarterEqualTo(record.getStatQuarter());
        }
        // ///////

        return familyIncomeDao.selectByArg(arg);
    }

    @Override
    public Page<FamilyIncomePO> selectByArgAndPage(FamilyIncomePO record,
            Page<FamilyIncomePO> resultPage) throws BaseAppException {
        logger.debug("selectByArgAndPage begin...record={0}", record);

        // 第一种方式：自己创建arg，自行设置查询条件及操作符
        FamilyIncomeArg arg = new FamilyIncomeArg();
        // TODO 根据业务场景，设置查询条件，示例
        FamilyIncomeCriteria criteria = arg.createCriteria();
        if (Utils.notEmpty(record.getFamilyId())) {
            criteria.andFamilyIdEqualTo(record.getFamilyId());
        }

        resultPage = familyIncomeDao.selectByArgAndPage(arg, resultPage);

        return resultPage;
    }

    @Override
    public int add(FamilyIncomePO record) throws BaseAppException {
        logger.debug("add begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行重名校验、设置主键、设置属性默认值等
        // 获取主键
        // int pkId = sequenceGenerator.sequenceIntValue("表名","主键名");
        // record.setUserId(pkId);
        // record.setCreatedDate(new Date());
        // ///////

        return familyIncomeDao.insertSelective(record);
    }

    @Override
    public int update(FamilyIncomePO record) throws BaseAppException {
        logger.debug("update begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行重名校验、数据有效性校验、设置属性默认值等

        // ///////

        return familyIncomeDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(FamilyIncomePO record) throws BaseAppException {
        logger.debug("delete begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行关联性校验、关联删除等

        // ///////

        return familyIncomeDao.deleteByPrimaryKey(record.getFamilyIncomeId());
    }

}
